package com.zj;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

public class Handle {
    //处理命令

    //1.学生端截图 screenshot
    public void screenshot(Socket socket) {
        try {
            Robot robot = new Robot();
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle screenRect = new Rectangle(screenSize);
            BufferedImage screenshot = robot.createScreenCapture(screenRect);

            // 先本地保存测试
            File localTest = new File("DEBUG_LOCAL_SCREENSHOT.png");
            ImageIO.write(screenshot, "png", localTest);
            System.out.println("调试：截图已本地保存到 " + localTest.getAbsolutePath());

            // 将截图写入字节数组
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(screenshot, "png", baos);
            byte[] imageData = baos.toByteArray();

            // 获取输出流
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

            // 1. 发送固定10字节协议头（无冒号）
            byte[] header = "SCREENSHOT".getBytes(StandardCharsets.UTF_8);
            dos.write(header);
            System.out.println("调试：发送协议头(10字节)");

            // 2. 发送文件大小（8字节）
            dos.writeLong(imageData.length);

            // 3. 发送图片数据
            dos.write(imageData);
            dos.flush();

            System.out.println("学生端截图已发送");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //2.学生端关机 shutdown
    public void shutdown(){
        System.out.println("学生端程序即将关闭");
        // 退出当前 Java 程序，0 表示正常退出
        System.exit(0);
    }

    //3.发送信息 sendMessage
    public void sendMessage(String message){
        if (message != null) {
            System.out.println("收到教师端消息: " + message);
        }

    }

    //4.接收信息 receiveMessage
    public void receiveMessage(){
        System.out.println("学生端接收信息");
    }



}
